/**
 * Copyright (C), 2001-2031, www.bosssof.com.cn
 * FileName: AnalyzeJsonUtil.java
 * Author: ckj
 * Date: 2024/7/22 下午12:30
 * Description:
 * 解析json
 * History:
 * Date          Author   Version  Desc
 * 2024-01-01    bosssoft  1.0.0   initialize this file
 */
package com.bosssoft.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bosssoft.constants.FileExtensionConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

import static com.alibaba.fastjson.JSON.parseObject;


/**
 * @author ckj
 */
public class AnalyzeJsonUtil {

    private static AnalyzeJsonUtil instance;
    private static final String ANALYZE_FILE = "src/main/java/com/bosssoft/analyzefile/";
    private static final Logger logger = LoggerFactory.getLogger(AnalyzeJsonUtil.class);

    /**
     *
     * @author ckj
     * @date 2024/07/22 下午3:30
     * 私有构造方法，防止外部实例化
     */
    private AnalyzeJsonUtil() {
        //null
    }

    /**
     *
     * @author ckj
     * @date 2024/07/22 下午3:33
     * 公共的静态方法用于获取单例实例
     * @return com.bosssoft.utils.AnalyzeJsonUtil
     */
    public static synchronized AnalyzeJsonUtil getInstance() {
        if (instance == null) {
            instance = new AnalyzeJsonUtil();
        }
        return instance;
    }

    /**
     *
     * @author ckj
     * @date 2024/07/22 下午4:29
     * 开始解析Json文档并且保存至analyzefile中
     * @return int
     */
    public int parseAndPrintJsonFromFile(String filePath, String userName) {
        String fileName = FileUtil.extractFileName(filePath);
        String newFileName = ANALYZE_FILE  + userName + File.separator + fileName;
        String analyzeFileName = newFileName + FileExtensionConstant.TXT_EXTENSION;

        if (!FileUtil.fileExist(analyzeFileName)) {
            logger.error("分析文件不存在");
            return 0;
        }

        try (FileWriter fileWriter = new FileWriter(analyzeFileName)) {
            try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
                StringBuilder jsonContent = new StringBuilder();
                String line;
                while ((line = reader.readLine())!= null) {
                    jsonContent.append(line);
                }
                JSONObject jsonObject = parseObject(jsonContent.toString());
                JSONObject citiesObject = jsonObject.getJSONObject("cities");
                JSONArray cityArray = citiesObject.getJSONArray("city");
                for (int i = 0; i < cityArray.size(); i++) {
                    JSONObject cityObject = cityArray.getJSONObject(i);
                    String d1 = cityObject.getString("d1");
                    String d2 = cityObject.getString("d2");
                    String d3 = cityObject.getString("d3");
                    String d4 = cityObject.getString("d4");
                    logger.info("编号: {}, 城市:{}, 区域:{}, 详细区域: {}", d1, d2, d3, d4);
                    fileWriter.write("编号: " + d1 + ", 城市: " + d2 + ", 区域: " + d3 + ", 详细区域: " + d4 + "\n");
                }
            } catch (IOException e) {
                logger.error("读取文件或解析 JSON 失败", e);
                return 0;
            }
            try {
                fileWriter.flush();
            } catch (IOException e) {
                logger.error("刷新文件写入缓冲区失败", e);
            }
            return 1;
        } catch (IOException e) {
            logger.error("创建文件写入器失败", e);
            return 0;
        }
    }
}

